import dataList from '../component/data.mjs';

export var eventBus = new Vue({
    created() {
        this.$on("addClick", this.addClick);
        this.$on("add", this.add);
        this.$on("del", this.del);
        this.$on("sub", this.sub);
        this.$on("orderChange", this.orderChange);
        this.$on("proChange", this.proChange);


    },
    data: {
        // 控制切换菜单的标志位
        toggleFlag: 'frequently-food',
        // 控制切换订单区的组件    0=current-order 1=pending-order  2=paid  3=cancelled-order
        orderFlag: 'current-order',
        // 用于存取当前订单的数据

        currentList: [],
        list:dataList,
    },
    computed: {
        indexlist() {
            return this.list.length
        } , 
        indexDoneList() {
            return this.doneList.length
        } , 
    },
  


    methods: {
        orderChange(name) {
            this.orderFlag = name;
        },
        proChange(name) {
            this.toggleFlag = name;
        },


        addClick(e) {
            // 先判断数组的长度 如果为空直接push
            if (this.currentList.length < 1) {
                this.currentList.push(e);
            } else {
                // 不为空判断每次加入的商品名称
                let flag = true;
                this.currentList.forEach((item, index) => {

                    if (item.text===e.text) {
                        flag = false;
                        item.nums++;
                    }
                })
                if (flag) {
                    this.currentList.push(e);

                }
            }

        },
        // 订单的事件函数
        add(index) {

            this.currentList[index].nums++;
        },
        sub(index) {

            if (this.currentList[index].nums < 2) {
                this.currentList.splice(index, 1);
            } else {
                this.currentList[index].nums--
            }
        },
        del(index) {

            this.currentList.splice(index, 1);
        }
    },

})